package com.sfy.les.frame;

import java.awt.Container;
import java.awt.Dimension;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;

import com.sfy.les.dictionary.DataBaseDictionary;
import com.sfy.les.dictionary.mysql.MysqlDataBaseDictionary;
import com.sfy.les.dictionary.oracle.OracleDataBaseDictionary;
import com.sfy.les.dictionary.sqlserver.SqlServerDataBaseDictionary;

public class MyFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	//顶层容器
    private Container con = null;
    //中间容器
    private JPanel jp = null;
    //其它容器
    //数据库类型
    private JLabel dbTypeLable = null;
    private JComboBox<String> dbTypeComBox = null;
    //数据库驱动
    private JLabel dbDriverLable = null;
    private JTextField dbDriverTextField = null;
    //JDBC连接url
    private JLabel dbUrlLable = null;
    private JTextField dbUrlTextField = null;
    //用户名
    private JLabel dbUserNameLable = null;
    private JTextField dbUserNameTextField = null;
    //密码
    private JLabel dbPasswordLable = null;
    private JTextField dbPasswordTextField = null;
    //要导出的数据库
    private JLabel dbNameLable = null;
    private JTextField dbNameTextField = null;
	//要导出的数据库
    private JLabel tableNamePrefixLable = null;
    private JTextField tableNamePrefixTextField = null;
    
    //文件导出目录
    private JLabel fileUrlLable = null;
    private JTextField fileUrlTextField = null;
    private JButton fileUrlButton = null;
    //文件名称
    private JLabel fileNameLable = null;
    private JTextField fileNameTextField = null;
    //导出按钮
    private JButton exportButton = null;
    //导出日志信息
    private static ConsoleTextArea logTextArea = null;
    
    //Box容器
    private Box box = Box.createVerticalBox();//创建纵向Box容器
    private Box box1 = Box.createHorizontalBox();//创建横向Box容器
    private Box box2 = Box.createHorizontalBox();//创建横向Box容器
    private Box box3 = Box.createHorizontalBox();//创建横向Box容器
    private Box box4 = Box.createHorizontalBox();//创建横向Box容器
    private Box box5 = Box.createHorizontalBox();//创建横向Box容器
    private Box box6 = Box.createHorizontalBox();//创建横向Box容器
    private Box box7 = Box.createHorizontalBox();//创建横向Box容器
    private Box box8 = Box.createHorizontalBox();//创建横向Box容器
    private Box box9 = Box.createHorizontalBox();//创建横向Box容器
    private Box box10 = Box.createHorizontalBox();//创建横向Box容器
    private Box box11 = Box.createHorizontalBox();//创建横向Box容器
    
    public MyFrame(String title) throws IOException {
        super(title);//调用父类JFrame的构造方法

        //初始化顶层容器Container和中间容器JPanel
        con = getContentPane();//顶层容器直接从JFrame获得
        jp = new JPanel();

        //创建盒式布局对象(容器实例的引用,组件的布局方式)
        BoxLayout bl = new BoxLayout(jp, BoxLayout.X_AXIS);//X_AXIS水平布置,Y_AXIS垂直布置
        
        //设置容器的布局
        jp.setLayout(bl);
        
        //初始化其它容器
        dbTypeLable = new JLabel("                   数据库类型：");
        dbTypeComBox = new JComboBox<String>();
        dbTypeComBox.addItem("MySql");
        dbTypeComBox.addItem("Oracle");
        dbTypeComBox.addItem("SqlServer");
        dbTypeComBox.setMaximumSize(new Dimension(Integer.MAX_VALUE, dbTypeComBox.getMinimumSize().height));
        dbTypeComBox.addActionListener(e -> onSelectChange());
        
        dbDriverLable = new JLabel("               数据库驱动类：");
        dbDriverTextField = new JTextField("com.mysql.cj.jdbc.Driver");
        dbDriverTextField.setEditable(false);
        dbDriverTextField.setMaximumSize(new Dimension(Integer.MAX_VALUE, dbDriverTextField.getMinimumSize().height));
        
        dbUrlLable = new JLabel("                 JDBC连接url：");
        dbUrlTextField = new JTextField("jdbc:mysql://127.0.0.1:3306/dbname");
        dbUrlTextField.setMaximumSize(new Dimension(Integer.MAX_VALUE, dbUrlTextField.getMinimumSize().height));
        
        dbUserNameLable = new JLabel("                            用户名：");
        dbUserNameTextField = new JTextField();
        dbUserNameTextField.setMaximumSize(new Dimension(Integer.MAX_VALUE, dbUserNameTextField.getMinimumSize().height));
        
        dbPasswordLable = new JLabel("                                密码：");
        dbPasswordTextField = new JTextField();
        dbPasswordTextField.setMaximumSize(new Dimension(Integer.MAX_VALUE, dbPasswordTextField.getMinimumSize().height));
        
        dbNameLable = new JLabel("  要导出的数据库名称：");
        dbNameTextField = new JTextField();
        dbNameTextField.setMaximumSize(new Dimension(Integer.MAX_VALUE, dbNameTextField.getMinimumSize().height));
        
        tableNamePrefixLable = new JLabel("           要导出的表前缀：");
        tableNamePrefixTextField = new JTextField();
        tableNamePrefixTextField.setMaximumSize(new Dimension(Integer.MAX_VALUE, tableNamePrefixTextField.getMinimumSize().height));
        
        fileUrlLable = new JLabel("               文件导出目录：");
        fileUrlTextField = new JTextField();
        fileUrlTextField.setEditable(false);
        fileUrlTextField.setMaximumSize(new Dimension(Integer.MAX_VALUE, fileUrlTextField.getMinimumSize().height));
        fileUrlButton = new JButton("选择");
        fileUrlButton.addActionListener(e -> onFileSelect());
        
        fileNameLable = new JLabel("                        文件名称：");
        fileNameTextField = new JTextField("数据字典");
        fileNameTextField.setMaximumSize(new Dimension(Integer.MAX_VALUE, fileNameTextField.getMinimumSize().height));
        
        exportButton = new JButton("导出");
        exportButton.addActionListener(e -> export());
        
        logTextArea = new ConsoleTextArea();
        logTextArea.setEditable(false);
        jp.add(box);
        box.add(box1);
        
        box1.setBorder(new EmptyBorder(5, 0, 0, 0));
        box1.add(dbTypeLable);
        box1.add(dbTypeComBox);
        box1.add(Box.createRigidArea(new Dimension(50, dbDriverTextField.getMinimumSize().height)));
        
        box.add(box2);
        box2.setBorder(new EmptyBorder(5, 0, 0, 0));
        box2.add(dbDriverLable);
        box2.add(dbDriverTextField);
        box2.add(Box.createRigidArea(new Dimension(50, dbDriverTextField.getMinimumSize().height)));
        
        box.add(box3);
        box3.setBorder(new EmptyBorder(5, 0, 0, 0));
        box3.add(dbUrlLable);
        box3.add(dbUrlTextField);
        box3.add(Box.createRigidArea(new Dimension(50, dbUrlTextField.getMinimumSize().height)));
        
        box.add(box4);
        box4.setBorder(new EmptyBorder(5, 0, 0, 0));
        box4.add(dbUserNameLable);
        box4.add(dbUserNameTextField);
        box4.add(Box.createRigidArea(new Dimension(50, dbUserNameTextField.getMinimumSize().height)));
        
        box.add(box5);
        box5.setBorder(new EmptyBorder(5, 0, 0, 0));
        box5.add(dbPasswordLable);
        box5.add(dbPasswordTextField);
        box5.add(Box.createRigidArea(new Dimension(50, dbPasswordTextField.getMinimumSize().height)));
        
        box.add(box6);
        box6.setBorder(new EmptyBorder(5, 0, 0, 0));
        box6.add(dbNameLable);
        box6.add(dbNameTextField);
        box6.add(Box.createRigidArea(new Dimension(50, dbNameTextField.getMinimumSize().height)));
        
        box.add(box11);
        box11.setBorder(new EmptyBorder(5, 0, 0, 0));
        box11.add(tableNamePrefixLable);
        box11.add(tableNamePrefixTextField);
        box11.add(Box.createRigidArea(new Dimension(50, tableNamePrefixTextField.getMinimumSize().height)));
        
        box.add(box7);
        box7.setBorder(new EmptyBorder(5, 0, 0, 0));
        box7.add(fileUrlLable);
        box7.add(fileUrlTextField);
        box7.add(fileUrlButton);
        box7.add(Box.createRigidArea(new Dimension(50, fileUrlTextField.getMinimumSize().height)));
        
        box.add(box8);
        box8.setBorder(new EmptyBorder(5, 0, 0, 0));
        box8.add(fileNameLable);
        box8.add(fileNameTextField);
        box8.add(Box.createRigidArea(new Dimension(50, fileNameTextField.getMinimumSize().height)));
        
        box.add(box9);
        box9.setBorder(new EmptyBorder(5, 0, 5, 0));
        box9.add(exportButton);
        
        box.add(box10);
        box10.setBorder(new EmptyBorder(5, 0, 5, 0));
        box10.add(new JScrollPane(logTextArea));
        
        con.add(jp);//给顶层容器

    }
    
    /**
     * 选择导出目录
     */
    private void onFileSelect() {
    	int result = 0;
		String path = null;
		JFileChooser fileChooser = new JFileChooser();
		//注意了，这里重要的一句
		FileSystemView fsv = FileSystemView.getFileSystemView();  
		//得到桌面路径
		fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
		fileChooser.setDialogTitle("请选择要导出的目录...");
		fileChooser.setApproveButtonText("确定");
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		result = fileChooser.showOpenDialog(fileChooser);
		if (JFileChooser.APPROVE_OPTION == result) {
			path=fileChooser.getSelectedFile().getPath();
			fileUrlTextField.setText(path);
	   }
    }
    
    /**
     * 下拉框值切换
     */
    private void onSelectChange(){
    	String dbType = dbTypeComBox.getSelectedItem().toString();
    	if("MySql".equals(dbType)){
    		dbDriverTextField.setText("com.mysql.cj.jdbc.Driver");
    		dbUrlTextField.setText("jdbc:mysql://127.0.0.1:3306/dbname");
    	}
    	if("Oracle".equals(dbType)){
    		dbDriverTextField.setText("oracle.jdbc.driver.OracleDriver");
    		dbUrlTextField.setText("jdbc:oracle:thin:@127.0.0.1:1521:sid");
    	}
    	if("SqlServer".equals(dbType)){
    		dbDriverTextField.setText("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    		dbUrlTextField.setText("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=dbname;SelectMethod=Cursor");
    	}
    }
    
    /**
     * 导出
     */
    private void export(){
    	new Thread(new Runnable() {
			public void run() {
				logTextArea.setText("");
				logTextArea.paintImmediately(logTextArea.getBounds());
		    	//获取各个组件的值
		    	DataBaseDictionary dataBaseDictionary = getFrameInfo();
		    	try {
					dataBaseDictionary.createExcel();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
    }
    
	private DataBaseDictionary getFrameInfo(){
		DataBaseDictionary dataBaseDictionary = null;
		//数据库类型
		String dbType = dbTypeComBox.getSelectedItem().toString();
    	if("MySql".equals(dbType)){
    		dataBaseDictionary = new MysqlDataBaseDictionary();
    	}
    	if("Oracle".equals(dbType)){
    		dataBaseDictionary = new OracleDataBaseDictionary();
    	}
    	if("SqlServer".equals(dbType)){
    		dataBaseDictionary = new SqlServerDataBaseDictionary();
    	}
    	//jdbc连接地址
    	dataBaseDictionary.setDbUrl(dbUrlTextField.getText());
    	//用户名
    	dataBaseDictionary.setDbUserName(dbUserNameTextField.getText());
    	//密码
    	dataBaseDictionary.setDbPassword(dbPasswordTextField.getText());
    	//要导出的数据库
    	dataBaseDictionary.setDbName(dbNameTextField.getText());
    	//导出目录
    	dataBaseDictionary.setFileUrl(fileUrlTextField.getText());
    	//导出文件名
    	dataBaseDictionary.setFileName(fileNameTextField.getText());
    	//表前缀
    	dataBaseDictionary.setTableNamePrefix(tableNamePrefixTextField.getText());
    	
    	return dataBaseDictionary;
	}
	
    
    public static void main(String[] args) {
		// JFrame指一个窗口，构造方法的参数为窗口标题
		try {
			MyFrame frame = new MyFrame("导出数据字典");
			//初始化页面布局
	        //注意以下两句的顺序!当没设置尺寸时,中央也就是左上角的位置,再去设置尺寸则左上角不动
			frame.setSize(750, 500);//尺寸
			frame.setLocationRelativeTo(null);//设置在屏幕中央
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//按关闭时退出
			//设置继承了JFrame类的本类对象的可见性
			frame.setVisible(true);
			frame.addWindowListener(new java.awt.event.WindowAdapter() {
	            public void windowClosing(java.awt.event.WindowEvent evt) {
	                System.exit(0);
	            }
	        });
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    
}



